using System;

using Tino.Core.Utility;

namespace Tino
{
    /// <summary>
    /// Singleton class that contains the global variables of the application
    /// </summary>
    public class Globals
    {
        static Globals instance;
        static readonly object padlock = new object();

        ConfigManager cfg;

        int x;
        public int X
        {
            get { return x; }
            set { x = value; }
        }

        int y;
        public int Y
        {
            get { return y; }
            set { y = value; }
        }

        int width;
        public int Width
        {
            get { return width; }
            set { width = value; }
        }

        int height;
        public int Height
        {
            get { return height; }
            set { height = value; }
        }

        int verticalSplit;
        public int VerticalSplit
        {
            get { return verticalSplit; }
            set { verticalSplit = value; }
        }

        int horizontalSplit;
        public int HorizontalSplit
        {
            get { return horizontalSplit; }
            set { horizontalSplit = value; }
        }

        string lastOpenedFile;
        public string LastOpenedFile
        {
            get { return lastOpenedFile; }
            set { lastOpenedFile = value; }
        }

        string lastSeenDirectory;
        public string LastSeenDirectory
        {
            get { return lastSeenDirectory; }
            set { lastSeenDirectory = value; }
        }

        bool showWelcome;
        public bool ShowWelcome
        {
            get { return showWelcome; }
            set { showWelcome = value; }
        }

        bool wordWrap;
        public bool WordWrap
        {
            get { return wordWrap; }
            set { wordWrap = value; }
        }

        protected Globals()
        {
            cfg = new ConfigManager();

            x = ensureIntPositive((int)cfg.GetValue("X", typeof(int), 0), 0);
            y = ensureIntPositive((int)cfg.GetValue("Y", typeof(int), 0), 0);
            width = ensureIntPositive((int)cfg.GetValue("Width", typeof(int), 650), 650);
            height = ensureIntPositive((int)cfg.GetValue("Height", typeof(int), 410), 410);
            verticalSplit = ensureIntPositive((int)cfg.GetValue("VerticalSplit", typeof(int), 180), 180);
            horizontalSplit = ensureIntPositive((int)cfg.GetValue("HorizontalSplit", typeof(int), 110), 110);
            lastOpenedFile = (string)cfg.GetValue("LastOpenedFile", typeof(string), "");
            lastSeenDirectory = (string)cfg.GetValue("LastSeenDirectory", typeof(string), "");
            showWelcome = (bool)cfg.GetValue("ShowWelcome", typeof(bool), true);
            wordWrap = (bool)cfg.GetValue("WordWrap", typeof(bool), true);
        }

        static public Globals Get()
        {
            lock (padlock)
            {
                if (instance == null)
                    instance = new Globals();
                return instance;
            }
        }

        public void Save()
        {
            cfg.SetValue("X", x);
            cfg.SetValue("Y", y);
            cfg.SetValue("Width", width);
            cfg.SetValue("Height", height);
            cfg.SetValue("VerticalSplit", verticalSplit);
            cfg.SetValue("HorizontalSplit", horizontalSplit);
            cfg.SetValue("LastOpenedFile", lastOpenedFile);
            cfg.SetValue("LastSeenDirectory", lastSeenDirectory);
            cfg.SetValue("ShowWelcome", showWelcome);
            cfg.SetValue("WordWrap", wordWrap);
        }

        int ensureIntPositive(int input, int def)
        {
            return (input > 0 ? input : def);
        }
    }
}
